<script type="text/javascript" src="../static/javascript/sea.js"></script>
<script type="text/javascript" src="../static/javascript/seajs-config.js"></script>
<script type="text/javascript">
	seajs.use(['seajs-control'], function(){
		$(document).seaLoad();
	});
	var SeaOptions = {
		seaTree:{
			data: {
				simpleData: {
					enable: true,
					idKey: "catid",
					pIdKey: "upid",
					rootPId: 0
				},
				key: {
					name: "catname",
					title: "description"
				}
			},
			edit: {
				enable: true,
				showRenameBtn: false
			},
			view:{
				addHoverDom: function(treeId, treeNode){
					var sObj = $("#" + treeNode.tId + "_span"),
						zTree = $.fn.zTree.getZTreeObj(sObj.closest('ul.ztree').attr('id'));
					if (treeNode.editNameFlag || $("#"+treeNode.tId+"_add").length>0 || $("#"+treeNode.tId+"_display").length>0 || $("#"+treeNode.tId+"_edit").length>0 ) return;
					var showStr = "<span class='button display' id='"+treeNode.tId+"_display' title='查看节点信息'></span>",
						addStr = "<span class='button add' id='"+treeNode.tId+"_add' title='添加新的节点'></span>",
						editStr = "<span class='button edit' id='"+treeNode.tId+"_edit' title='编辑节点信息'></span>";
					sObj.after(showStr + addStr + editStr);
					var addBtn = $("#"+treeNode.tId+'_add'),
							displayBtn = $("#"+treeNode.tId+'_display'),
							editBtn = $("#"+treeNode.tId+'_edit');
					if (addBtn) addBtn.bind("click", function(){
						if(typeof zTree.setting.callback.onAdd == 'function') zTree.setting.callback.onAdd(zTree, treeId, treeNode);
						return false;
					});
					if (displayBtn) displayBtn.bind("click", function(){
						if(typeof zTree.setting.callback.onDisplay == 'function') zTree.setting.callback.onDisplay(zTree, treeId, treeNode);
						return false;
					});
					if (editBtn) editBtn.bind("click", function(){
						if(typeof zTree.setting.callback.onEdit == 'function') zTree.setting.callback.onEdit(zTree, treeId, treeNode);
						return false;
					});
				},
				removeHoverDom: function(treeId, treeNode){
					$("#"+treeNode.tId+'_display').unbind().remove();
					$("#"+treeNode.tId+'_add').unbind().remove();
					$("#"+treeNode.tId+'_edit').unbind().remove();
				}
			},
			callback: {
				onAdd: function(zTree, treeId, treeNode){
					var form = $('#categoryForm');
					form.find('thead strong').text('添加新的题库分类');
					form.find('select').val(treeNode.catid).removeAttr('disabled');
					form.find('input:eq(0)').val('').removeAttr('disabled');
					form.find('input:eq(1)').val(0).removeAttr('disabled');
					form.find('input:eq(2)').val(0).removeAttr('disabled');
					form.find('textarea').val('').removeAttr('disabled');
					form.find('[name=apply_action]:input').val('Add');
					$('[name=catid]:input', form).remove();
					form.find('tfoot').show();
				},
				onDisplay: function(zTree, treeId, treeNode){
					var form = $('#categoryForm');
					form.find('thead strong').text('查看【'+treeNode.catname+'】题库分类信息');
					form.find('select').val(treeNode.upid).attr('disabled', 'disabled');
					form.find('input:eq(0)').val(treeNode.catname).attr('disabled', 'disabled');
					form.find('input:eq(1)').val(treeNode.displayorder).attr('disabled', 'disabled');
					form.find('input:eq(2)').val(treeNode.weight).attr('disabled', 'disabled');
					form.find('textarea').val(treeNode.description).attr('disabled', 'disabled');
					form.find('tfoot').hide();
				},
				onEdit: function(zTree, treeId, treeNode){
					var form = $('#categoryForm');
					form.find('thead strong').text('编辑【'+treeNode.catname+'】题库分类信息');
					form.find('select').val(treeNode.upid).removeAttr('disabled');
					form.find('input:eq(0)').val(treeNode.catname).removeAttr('disabled');
					form.find('input:eq(1)').val(treeNode.displayorder).removeAttr('disabled');
					form.find('input:eq(2)').val(treeNode.weight).removeAttr('disabled');
					form.find('textarea').val(treeNode.description).removeAttr('disabled');
					form.find('[name=apply_action]:input').val('Edit');
					form.append('<input type="hidden" name="catid" value="'+treeNode.catid+'">');
					form.find('tfoot').show();
				},
				beforeRemove: function(treeId, treeNode){
					if(confirm("确认删除 题库分类 -- 【" + treeNode.catname + "】 吗？")){
						$.post($('#categoryForm').attr('action'), {
							catid: treeNode.catid,
							apply_action: 'Del',
							formhash: $('#categoryForm').find('[name=formhash]:input').val()
						})
					}else{
						return false;
					}
				}
			},
			async: { enable: false},
			expand: 'all',
			nodes: {$nodes}
		},
		validator:{
			rules: {
				upid: { required: true},
				catname: { required: true}
			},
			messages: {
				upid: { required:'请选择上级分类！'},
				catname: { required:'分类名称必须填写！'}
			}
		}
	};
</script>
<table class="tb tb2 " id="sea">
	<tbody>
	<tr>
		<td width="50%">
			<ul data-sea="seaTree" data-config="seaTree"></ul>
		</td>
		<td width="50%">
			<form method="post" action="./admin.php?action=plugins&operation=config&do={$pluginid}&identifier=iexam&pmod=category" data-sea="Validator" id="categoryForm">
				<table class="tb tb2 ">
					<thead>
					<tr>
						<th colspan="2"><strong>试题分类管理</strong></th>
					</tr>
					</thead>
					<tbody>
					<tr>
						<td>上级分类</td>
						<td><select name="upid">
							<option value="0">+顶级分类菜单</option>
							<!--{loop $category[select] $key $val}-->
							<option value="{$val[catid]}">{$val[catname]}</option>
							<!--{/loop}-->
						</select></td>
					</tr>
					<tr>
						<td>分类名称</td>
						<td><input type="text" name="catname"></td>
					</tr>
					<tr>
						<td>分类排序</td>
						<td><input type="text" name="displayorder" value="0"></td>
					</tr>
					<tr>
						<td>试题权重</td>
						<td><input type="text" name="weight" value="0"></td>
					</tr>
					<tr>
						<td>分类说明</td>
						<td><textarea name="description"></textarea></td>
					</tr>
					</tbody>
					<tfoot>
					<tr>
						<td colspan="2">
							<button name="apply_submit" class="pn" type="submit"><em>提交保存</em></button>
						</td>
					</tr>
					</tfoot>
				</table>
				<input type="hidden" name="apply_action" value="Add">
				<input type="hidden" name="formhash" value="{FORMHASH}">
			</form>
		</td>
	</tr>
	</tbody>
</table>